package com.cxm.helper.controller.admin;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.cxm.helper.pojo.User;
import com.cxm.helper.pojo.dto.UserListDTO;
import com.cxm.helper.pojo.dto.UserLoginDTO;
import com.cxm.helper.pojo.vo.UserVO;
import com.cxm.helper.service.UserService;
import com.cxm.helper.util.ResponseEntity;
import com.cxm.helper.util.response.PageResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: chenximeng
 * @date: 2022/11/20 11:21
 */
@RestController
@RequestMapping("/admin/user")
@SaCheckRole("admin")
public class UserAdminController {

    @Autowired
    UserService userService;

    @PostMapping("/list")
    public ResponseEntity<PageResponse> list(@RequestBody UserListDTO userListDTO) {
        return ResponseEntity.ok(userService.list(userListDTO));
    }

    @PostMapping("/login")
    public ResponseEntity login(@RequestBody UserLoginDTO dto) {
        UserVO userVO = userService.login(dto.getPhone(), dto.getPassword(), true);
        StpUtil.login(userVO.getId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        userVO.setTokenName(tokenInfo.getTokenName());
        userVO.setTokenValue(tokenInfo.getTokenValue());
        return ResponseEntity.ok(userVO, "登陆成功");
    }

    @GetMapping("/changeStatus/{id}")
    public ResponseEntity updateStatus(@PathVariable Long id){
        return ResponseEntity.ok(userService.updateStatus(id));
    }

    @GetMapping("/changeRole/{id}")
    public ResponseEntity updateRole(@PathVariable Long id){
        return ResponseEntity.ok(userService.updateRole(id));
    }
}
